package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.UserEmployerInfo;
import cn.anxingongyou.anxin.entity.UserWorkerInfo;
import cn.anxingongyou.anxin.entity.Work;
import cn.anxingongyou.anxin.utils.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class SearchController {

    private final ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    public SearchController(ElasticsearchTemplate elasticsearchTemplate) {
        this.elasticsearchTemplate = elasticsearchTemplate;
    }

    @ApiOperation("搜索工作")
    @ApiResponses({
            @ApiResponse(code = 200,message = "搜索成功"),
            @ApiResponse(code = 500,message = "搜索失败")
    })
    @RequestMapping(value = "/search/work/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Map searchWork(
            @PathVariable("pageNum") @ApiParam(value = "页码，0开始", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页条数", required = true) int pageSize,
            @RequestParam("key") @ApiParam(value = "关键字", required = true) String key
    ){
        // 创建查询对象
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(key)
                        .field("employeeName")
                        .field("workType")
                        .field("workName")
                        .field("contacts")
                        .field("workPlace")
                        .field("workDetail")
                        .field("label")
                        .field("workEducation")
                        .field("workExperience")
                        .field("workAge")
                        .field("workEndTime")
                        .field("workStartDate")
                        .field("phone")
                        .field("workEndDate")
                        .field("workStartTime")
                )
                .withPageable(PageRequest.of(pageNum,pageSize))
                .build();
        // 执行查询
        List<Work> list = elasticsearchTemplate.queryForList(query, Work.class);
        return Result.<List<Work>>instance(200,"搜索成功",list).toJsonMap();
    }

    @ApiOperation("搜索工人")
    @ApiResponses({
            @ApiResponse(code = 200,message = "搜索成功"),
            @ApiResponse(code = 500,message = "搜索失败")
    })
    @RequestMapping(value = "/search/worker/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Map searchWorker(
            @PathVariable("pageNum") @ApiParam(value = "页码，0开始", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页条数", required = true) int pageSize,
            @RequestParam("key") @ApiParam(value = "关键字", required = true) String key
    ){
        // 创建查询对象
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(key)
                        .field("education")
                        .field("label")
                        .field("experience")
                        .field("speciality")
                        .field("phone")
                        .field("region")
                        .field("age")
                        .field("introduction")
                        .field("username")
                )
                .withPageable(PageRequest.of(pageNum,pageSize))
                .build();
        // 执行查询
        List<UserWorkerInfo> list = elasticsearchTemplate.queryForList(query, UserWorkerInfo.class);
        return Result.<List<UserWorkerInfo>>instance(200,"搜索成功",list).toJsonMap();
    }

    @ApiOperation("搜索招聘者")
    @ApiResponses({
            @ApiResponse(code = 200,message = "搜索成功"),
            @ApiResponse(code = 500,message = "搜索失败")
    })
    @RequestMapping(value = "/search/employer/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Map searchEmployer(
            @PathVariable("pageNum") @ApiParam(value = "页码，0开始", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页条数", required = true) int pageSize,
            @RequestParam("key") @ApiParam(value = "关键字", required = true) String key
    ){
        // 创建查询对象
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(key)
                        .field("address")
                        .field("nature")
                        .field("introduce")
                        .field("scale")
                        .field("industry")
                        .field("label")
                        .field("phone")
                        .field("enterpriseName")
                )
                .withPageable(PageRequest.of(pageNum,pageSize))
                .build();
        // 执行查询
        List<UserEmployerInfo> list = elasticsearchTemplate.queryForList(query, UserEmployerInfo.class);
        return Result.<List<UserEmployerInfo>>instance(200,"搜索成功",list).toJsonMap();
    }

}
